package com.example.administrator.jinagsu2021_2;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Map;

import cn.com.newland.nle_sdk.requestEntity.SignIn;
import cn.com.newland.nle_sdk.responseEntity.SensorInfo;
import cn.com.newland.nle_sdk.responseEntity.User;
import cn.com.newland.nle_sdk.responseEntity.base.BaseResponseEntity;
import cn.com.newland.nle_sdk.util.NCallBack;
import cn.com.newland.nle_sdk.util.NetWorkBusiness;
import retrofit2.Call;

public class MainActivity extends AppCompatActivity {
    private NetWorkBusiness netWorkBusiness;
    private String accessToken,temp,hum,pm;
    private Button btn_setting;
    private TextView tv_temp,tv_hum,tv_pm,tv_status;
    private Double tempd;
    private Double humd;
    private Double pmd;
    private Double maxtemp,maxhum;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        String acc = SaveandGet.getacc(MainActivity.this);
        netWorkBusiness=new NetWorkBusiness(acc,"http://api.nlecloud.com:80");
        Mythread mythread =new Mythread();
        mythread.start();
        btn_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,Setting.class);
                startActivity(intent);

            }
        });

    }
    private void init(){
        tempd=0.0;
        humd=0.0;
        pmd=0.0;
        maxhum=25.0;
        maxtemp=25.0;
        btn_setting=findViewById(R.id.btn_setting);
        tv_temp=findViewById(R.id.tv_temp);
        tv_hum=findViewById(R.id.tv_hum);
        tv_pm=findViewById(R.id.tv_pm);
        tv_status=findViewById(R.id.tv_status);
        String path="http://api.nlecloud.com:80/";
        netWorkBusiness=new NetWorkBusiness("",path);
        netWorkBusiness.signIn(new SignIn("13646795937", "jhc210210"), new NCallBack<BaseResponseEntity<User>>(this) {
            @Override
            protected void onResponse(BaseResponseEntity<User> response) {
                if(response!=null){
                    if(response.getStatusCode()==0){
                        SaveandGet.saveAcc(response.getResultObj().getAccessToken(),MainActivity.this);
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseResponseEntity<User>> call, Throwable t) {
                Toast.makeText(MainActivity.this,"登陆失败"+t.getMessage(),Toast.LENGTH_SHORT
                ).show();
            }
        });
    }



    private void control(String deviceid, final String api, Object value){
        netWorkBusiness.control(deviceid, api, value, new NCallBack<BaseResponseEntity>(this) {
            @Override
            protected void onResponse(BaseResponseEntity response) {
                if(response!=null){
                    Log.i("main",api);
                }
            }
        });
    }
    private void gettemp(){
        netWorkBusiness.getSensor("95498","z_temperature", new NCallBack<BaseResponseEntity<SensorInfo>>(this) {
            @Override
            protected void onResponse(BaseResponseEntity<SensorInfo> response) {
                if(response!=null){
                    if(response.getStatusCode()==0){
                        final SensorInfo sensorInfo = response.getResultObj();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                tv_temp.setText(sensorInfo.getValue());
                                Log.i("aaa",sensorInfo.getValue());
                            }
                        });
                        tempd= Double.parseDouble(sensorInfo.getValue());
                    }
                }
            }
        });
    }
    private void gethum(){
        netWorkBusiness.getSensor("95498","z_humidity", new NCallBack<BaseResponseEntity<SensorInfo>>(this) {
            @Override
            protected void onResponse(BaseResponseEntity<SensorInfo> response) {
                if(response!=null){
                    if(response.getStatusCode()==0){
                        final SensorInfo sensorInfo = response.getResultObj();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                tv_hum.setText(sensorInfo.getValue());
                                Log.i("bbb",sensorInfo.getValue());
                            }
                        });
                        humd= Double.parseDouble(sensorInfo.getValue());
                    }
                }
            }
        });
    }
    private void getpm(){
        netWorkBusiness.getSensor("95498","z_air_quality", new NCallBack<BaseResponseEntity<SensorInfo>>(this) {
            @Override
            protected void onResponse(BaseResponseEntity<SensorInfo> response) {
                if(response!=null){
                    if(response.getStatusCode()==0){
                        final SensorInfo sensorInfo = response.getResultObj();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                tv_pm.setText(sensorInfo.getValue());
                                Log.i("ccc",sensorInfo.getValue());
                            }
                        });
                        pmd= Double.parseDouble(sensorInfo.getValue());
                    }
                }
            }
        });
    }
    private class Mythread extends Thread{
        @Override
        public void run() {
            super.run();
            while (true){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                gethum();
                getpm();
                gettemp();
                Map<String, String> map = SaveandGet.get(MainActivity.this);
                Log.i("main",map + "");
                if(map.get("maxtemp")!=null &&!map.get("maxtemp").equals("")){
                    maxtemp=Double.parseDouble(map.get("maxtemp"));
                    maxhum=Double.parseDouble(map.get("maxhum"));
                    Log.i("main",maxtemp+""); //这里的数据获取不到，我把它存导sp里面就是设置一个最大值然后跳回来，这里获取不到 不知道为啥我运行给你看
                }
                if(humd>maxhum||tempd>maxtemp){
                    control("95498","z_fan",1);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tv_status.setText("开");
                        }
                    });
                }else {
                    control("95498","z_fan",0);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tv_status.setText("关");
                        }
                    });
                }
            }
        }
    }

}
